// 封装分类列表数据

import { ref, onMounted } from 'vue'
import { onBeforeRouteUpdate, useRoute } from 'vue-router';
import { getCategoryAPI } from '@/apis/category';



export function useCategory() {

    const route = useRoute()

    // 数据
    const categoryData = ref({})
    // 方法
    const getCategory = async (id = route.params.id) => {
        const res = await getCategoryAPI(id)
        if (res.code === '1') categoryData.value = res.result
    }


    onBeforeRouteUpdate((to) => {
        getCategory(to.params.id)
    })

    onMounted(() => { getCategory() })

    return { categoryData }
}